# @Time : 2025/4/19 1:46
# @Author : ZhangQi

import json

from adb.device_manager import DeviceManager


def main():
    # 初始化设备连接
    device_id = "emulator-5554"

    try:
        agent = DeviceManager(device_id=device_id)
        print(f"\n✅ 设备连接成功 [{device_id}]")
        print("支持指令类型示例：\n"
              "1. 关闭应用包名（如：关闭com.example.app）\n"
              "2. 点击操作（如：点击[100,200]）\n"
              "3. 文本输入（如：输入Hello World）\n"
              "4. 滑动操作（如：滑动[100,200]->[300,400]）")
    except Exception as e:
        print(f"设备连接失败: {str(e)}")
        return

    # 指令循环处理
    while True:
        try:
            print("\n" + "=" * 40)
            command = input("请输入指令 (输入q退出): ").strip()

            if command.lower() in ['q', 'quit', 'exit']:
                break

            if not command:
                print("错误：指令不能为空")
                continue

            # 执行原始指令
            result = agent.process_command(command)

            # 结果格式化输出
            print("\n↘️ 执行结果：")
            print(json.dumps(result, indent=2, ensure_ascii=False))

        except KeyboardInterrupt:
            print("\n操作已中止")
            break
        except Exception as e:
            print(f"❌ 执行异常：{str(e)}")
            if input("是否重试？(y/n): ").lower() != 'y':
                break


if __name__ == "__main__":
    main()
